package com.htdata.iiot.tools;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
import java.util.concurrent.atomic.LongAdder;

import com.htdata.iiot.db.DbConnectionPool;
import com.htdata.iiot.db.JDBCUtils;

public class Auto2Mqtt {
	private static DbConnectionPool pool = DbConnectionPool.getPool();
	static long num = 0;
	static long threads = 20;
	static int	tid = 0;
	static int  did = 0;
	static long mid = 0;
	static boolean flag = false;
	static long st = System.currentTimeMillis();
	static String url = "192.168.0.81:1883";
	static boolean dataFlag = true;
	static LongAdder mAdder = new LongAdder();
	static LongAdder adder = new LongAdder();

	public static void main(String[] args) throws InterruptedException {
		for (String arg : args) {
			String[] split = arg.split("=");
			switch (split[0]) {
			case "threads":
				threads = Integer.parseInt(split[1]);
				break;
			case "url":
				url = split[1];
				break;
			case "tid":
				tid = Integer.parseInt(split[1]);
				break;
			case "did":
				did = Integer.parseInt(split[1]);
				break;
			case "mid":
				mid = Integer.parseInt(split[1]);
				break;
			case "sendType":
				dataFlag = (split[1].equals("status"))?false:true;
				break;
			case "sy":
				if(split[1].equals("1")){
					flag = true;
				}else{
					flag = false;
				}
				break;
			default:
				break;
			}
		}
		
		ArrayList<ArrayList<Object>> sList = new ArrayList<ArrayList<Object>>();

		String sql1 = "SELECT t.id AS tid, d.id AS id, d.serial_number AS dsn FROM tenant t, thing_device d"
				+ " WHERE t.deleted=0 AND d.tenant_id=t.id AND d.deleted=0 AND d.type=0";

		if(tid != 0)
		{
			sql1 += " AND t.id=" + tid;
		}
		if(did != 0)
		{
			sql1 += " AND d.id=" + did;
		}
		Connection conn = pool.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		ResultSet executeQuery = null;
		PreparedStatement prepareStatement = null;
		
		try {
			ps = conn.prepareStatement(sql1);
			rs = ps.executeQuery();
			while (rs.next()) {
				int tid = rs.getInt("tid");
				int deviceId = rs.getInt("id");
				String dsn = rs.getString("dsn");
				if (dsn == null || dsn.isEmpty()) {
					continue;
				}

				HashMap<String, Object> map = new HashMap<String, Object>();
				map.put("ts", 0);
				map.put("dn", dsn);
				if(!dataFlag){
					map.put("status", 1000);
				}
				else{
					// 2.鏍规嵁璁惧鑾峰彇娴嬬偣
					String sql2 = "SELECT t1.id AS id, t3.name AS name FROM"
							+ " thing_device_component_rel t1, thing_component t2, thing_data_stream t3"
							+ " WHERE t1.device_id=? AND t1.deleted=0 "
							+ "   AND t2.id=t1.component_id AND t2.tenant_id=? AND t2.deleted=0 "
							+ "   AND t3.component_id=t2.id AND t3.deleted=0";

					if(mid != 0)
					{
						sql2 += " AND t1.id=" + mid;
					}
					prepareStatement = conn.prepareStatement(sql2);
					prepareStatement.setInt(1, deviceId);
					prepareStatement.setInt(2, tid);
					executeQuery = prepareStatement.executeQuery();
					if (executeQuery.next()) {
						while (true) {
							String name = executeQuery.getString("name");
							if (name == null || name.isEmpty()) {
								map = null;
								break;
							}

							map.put(name, 0);

							if (!executeQuery.next())
								break;
						}
					} else {
						map = null;
					}
				}
				

				if (map != null) {
					ArrayList<Object> al = new ArrayList<Object>();
					al.add(tid);
					al.add(map);
					sList.add(al);
					map = null;
				}
				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtils.close(ps, rs, conn);
			JDBCUtils.close(prepareStatement, executeQuery, null);
		}

		
		long startTime = System.currentTimeMillis();

		for (int a = 0; a < threads; a++) {
			new Thread(new Post(sList, url, String.format("mqttThread-%04d", a))).start();
		}

		new Thread(new Runnable() {

			@Override
			public void run() {
				while (true) {
					try {
						Thread.sleep(5000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					
					long i = adder.sum();
					long mi = mAdder.sum();

					long endTime = System.currentTimeMillis();
					long useTime = endTime - startTime;
					System.out.println("条数： " + i + "测点数：" + mi + "平均条数 " + (i * 1000 / useTime) + "平均测点数 "
							+ (mi * 1000 / useTime));
				}
			}
		}).start();
	}

}
